package com.chang.multithread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.locks.LockSupport;
@Slf4j  //abc 按顺序交替输出5次
public class abcParkUnpark {
    private int loopNumber;

    public abcParkUnpark(int loopNumber) {
        this.loopNumber = loopNumber;
    }

    public void print(String str, Thread next){
        for (int i = 0; i <loopNumber ; i++) {
            LockSupport.park();
            System.out.print(str);
            LockSupport.unpark(next);
        }
    }
        static Thread t1;
        static Thread t2;
        static Thread t3;
    public static void main(String[] args) {
            abcParkUnpark pu=new abcParkUnpark(5);
            t1=new Thread(()->{
                pu.print("a", t2);
            });
            t2=new Thread(()->{
                pu.print("b", t3);
            });
            t3=new Thread(()->{
                pu.print("c", t1);
            });

            t1.start();
            t2.start();
            t3.start();
            log.debug("主线程发起");
            LockSupport.unpark(t1);

    }
}
